package Demo2;

/**
 *
 * @param <T> 当前类 是一个泛型  他只是一个占位符
 */
class MyArray<T>{
//    public T[] arr = new T[10];
    public Object[] array = new Object[10];

    public void setArray(int pos,T val){
        array[pos] = val;
    }

    public T getArray(int pos){
        return (T) array[pos];   //返回的类型  强转为指定的类型
    }
}
public class Test {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<Integer>();
        myArray.setArray(0,10);
        myArray.setArray(1,100);

        int a = myArray.getArray(1);
        int b= myArray.getArray(0);
        System.out.println(b);
        System.out.println(a);

        MyArray<String> myArray2 = new MyArray<>();
        myArray2.setArray(0,"abcnd");
        myArray2.setArray(1,"afsdfbcnd");
        String ret =myArray2.getArray(0);
        String ret1 =myArray2.getArray(1);
        System.out.println(ret1);
        System.out.println(ret);
    }
}
